package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/10/01 16:05
 **/
public class Day30_1_Num1694_ReformatNumber {
    class Solution {
        public String reformatNumber(String number) {
            number = number.replace(" ", "").replace("-", "");
            int n = number.length();
            StringBuffer sb = new StringBuffer();
            for(int i = 0; i < n; i += 3){
                if(i != 0) sb.append("-");
                if(i + 5 > n){  // 1. i 是下标位置，所以+1、+4之后才是最多倒数4个数
                    // 2. 处理最后剩余2个或者3个数的情况
                    if(i + 3 >= n) sb.append(number.substring(i));
                        // 3. 处理最后剩余4个数的情况
                    else sb.append(number.substring(i, i+2)).append("-").append(number.substring(i+2));
                    break;
                }
                sb.append(number.substring(i, i + 3));
            }
            return sb.toString();
        }
    }
}